package haltica.com.mx.ape.daos;

import haltica.com.mx.ape.domain.UsuarioSistema;

import javax.persistence.TypedQuery;

import org.springframework.dao.EmptyResultDataAccessException;

privileged aspect UsuarioSistemaDao {
	
	public static UsuarioSistema UsuarioSistema.findUsuarioSistemaByUsername(String username) {
		String query;
		TypedQuery<UsuarioSistema> q;
		
		if (username == null)
			return null;
		
		query = "SELECT o FROM UsuarioSistema AS o where o.username = :username";
		
		try {
			q = UsuarioSistema.entityManager().createQuery(query, UsuarioSistema.class);
			q.setParameter("username", username);
	        
	        return q.getSingleResult();
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		
        
    }
	
	
	public static boolean UsuarioSistema.existUsuarioUserName(String username) {
		String query;
		TypedQuery<UsuarioSistema> q;
		
		if (username == null)
			return false;
		
		query = "SELECT o FROM UsuarioSistema AS o where o.username = :username";
		
		try {
			q = UsuarioSistema.entityManager().createQuery(query, UsuarioSistema.class);
			q.setParameter("username", username);
			
			
			return q.getSingleResult() != null;
			
		} catch (EmptyResultDataAccessException e) {
			return false;
		}
		
    }

}
